package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.AgeDomain;
import service.AlexaUpdate;
import service.GetPage;
import service.GooglePageRank;
import service.Whois;

@SuppressWarnings("serial")
public class UpdateServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("utf-8");

		String name = Utils.fomatDomain(req.getParameter("name"));
		
		Domain domain = new Domain();
		domain.setName(name);
		domain.setDomain(Utils.returnDomain(name));
		
		try {
			new GetPage(domain);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			new GooglePageRank(domain);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			new AgeDomain(domain);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			new AlexaUpdate(domain);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			new Whois(domain);
		} catch (Exception e) {
			// TODO: handle exception
		}

		try {
			Thread.sleep(10000);
			resp.getWriter().println(domain.getName()+"<br/>");
			resp.getWriter().println(domain.getWotTrust()+"<br/>");
			resp.getWriter().println(domain.getAlexa()+"<br/>");
			resp.getWriter().println(domain.getGoogle()+"<br/>");
			resp.getWriter().println(domain.getIp()+"<br/>");
			resp.getWriter().println(domain.getSpeedSite()+"<br/>");
			resp.getWriter().println(domain.getAgeDomain()+"<br/>");
			resp.getWriter().println(domain.getCharset()+"<br/>");
			resp.getWriter().println(domain.getLanguage()+"<br/>");
			resp.getWriter().println(domain.getServer()+"<br/>");
			resp.getWriter().println(domain.getJsLib()+"<br/>");
			resp.getWriter().println(domain.getOutlink()+"<br/>");
			resp.getWriter().println(domain.getWhois()+"<br/>");
			resp.getWriter().println(domain.getWhoisIp()+"<br/>");
			resp.getWriter().println(domain.getTitle()+"<br/>");
			resp.getWriter().println(domain.getDescription()+"<br/>");
			resp.getWriter().println(domain.getKeyword()+"<br/>");
			resp.getWriter().println(domain.getAdsend()+"<br/>");
			resp.getWriter().println(domain.getAnalytics()+"<br/>");
			resp.getWriter().println(domain.getVisit()+"<br/>");
			
			URL url = new URL("http://1.evaluationsites.appspot.com/post-data");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write("name="+domain.getName());
            writer.write("&wotTrust="+domain.getWotTrust());
            writer.write("&wotVendor="+domain.getWotVendor());
            writer.write("&wotPrivacy="+domain.getWotPrivacy());
            writer.write("&wotChild="+domain.getWotChild());
            writer.write("&alexa="+domain.getAlexa());
            writer.write("&google="+domain.getGoogle());
            writer.write("&ip="+domain.getIp());
            writer.write("&speedSite="+domain.getSpeedSite());
            writer.write("&ageDomain="+domain.getAgeDomain());
            writer.write("&charset="+domain.getCharset());
            writer.write("&language="+domain.getLanguage());
            writer.write("&server="+domain.getServer());
            writer.write("&jsLib="+domain.getJsLib());
            writer.write("&outlink="+domain.getOutlink());
            writer.write("&whois="+domain.getWhois());
            writer.write("&whoisIp="+domain.getWhoisIp());
            writer.write("&title="+domain.getTitle());
            writer.write("&description="+domain.getDescription());
            writer.write("&keyword="+domain.getKeyword());
            writer.write("&domain="+domain.getDomain());
            writer.write("&adsend="+domain.getAdsend());
            writer.write("&analytics="+domain.getAnalytics());
            writer.write("&visit="+domain.getVisit());
            writer.close();
    
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            }
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
